A Comprehensive Beginner's Guide
C is a general-purpose, procedural programming language developed in the early 1970s by Dennis Ritchie at Bell Labs. It was originally designed for implementing the UNIX operating system. C is one of the most widely used programming languages of all time, and its influence is evident in many modern languages like C++, Java, and C#.
Note: C is often called a "middle-level" language because it combines elements of high-level languages with the functionality of assembly language.
Development of UNIX operating system begins at Bell Labs
Dennis Ritchie develops C language based on B language (which was based on BCPL)
The C Programming Language book by Brian Kernighan and Dennis Ritchie (K&R C) is published
ANSI standardizes C language (ANSI C or C89)
ISO adopts ANSI C standard (C90)
C99 standard introduces new features like inline functions, variable-length arrays, etc.
C11 standard adds multi-threading support, anonymous structures, and other features
C17 standard introduces bug fixes and minor improvements
| Feature | Description |
|---|---|
| Procedural Language | Programs are organized as sequences of instructions |
| Portability | C programs can run on different platforms with minimal changes |
| Efficiency | C produces highly efficient and fast executable code |
| Modularity | Programs can be divided into smaller functions and modules |
| Extensibility | New features can be added easily |
| Rich Standard Library | Provides numerous built-in functions |
| Pointers | Direct memory manipulation for efficient programming |
| Recursion | Functions can call themselves to solve problems |
// Preprocessor directives
#include <stdio.h>
#define MAX_VALUE 100
// Global declarations
int global_var = 10;
// Function prototypes
int add_numbers(int a, int b);
// Main function - program entry point
int main() {
// Local variables
int a = 5, b = 10;
int result;
// Statements
result = add_numbers(a, b);
// Output
printf("The sum is: %d\n", result);
return 0;
}
// User-defined functions
int add_numbers(int a, int b) {
return a + b;
}
Data types define the type of data a variable can hold. C has several built-in data types:
| Data Type | Size (bytes) | Range | Description |
|---|---|---|---|
| char | 1 | -128 to 127 or 0 to 255 | Character or small integer |
| int | 2 or 4 | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 | Integer numbers |
| float | 4 | 1.2E-38 to 3.4E+38 | Single-precision floating point |
| double | 8 | 2.3E-308 to 1.7E+308 | Double-precision floating point |
| void | 0 | N/A | Represents no type |
Modifiers can alter the meaning of basic data types:
#include <stdio.h>
int main() {
char grade = 'A';
int age = 25;
float height = 5.9;
double salary = 50000.50;
printf("Grade: %c\n", grade);
printf("Age: %d\n", age);
printf("Height: %.1f\n", height);
printf("Salary: %.2f\n", salary);
return 0;
}
Variables are named memory locations that store data which can be changed during program execution.
// Variable declaration
int count;
// Variable initialization
count = 10;
// Declaration and initialization
float price = 99.99;
// Multiple variables
int x = 5, y = 10, z = 15;
Constants are fixed values that cannot be changed during program execution.
// Using const keyword
const float PI = 3.14159;
// Using #define preprocessor
#define MAX_SIZE 100
// Literal constants
int days_in_week = 7;
char first_letter = 'A';
Operators are symbols that perform operations on variables and values.
| Operator | Description | Example |
|---|---|---|
| + | Addition | a + b |
| - | Subtraction | a - b |
| * | Multiplication | a * b |
| / | Division | a / b |
| % | Modulus (remainder) | a % b |
| Operator | Description | Example |
|---|---|---|
| == | Equal to | a == b |
| != | Not equal to | a != b |
| > | Greater than | a > b |
| < | Less than | a < b |
| >= | Greater than or equal to | a >= b |
| <= | Less than or equal to | a <= b |
| Operator | Description | Example |
|---|---|---|
| && | Logical AND | a && b |
| || | Logical OR | a || b |
| ! | Logical NOT | !a |
#include <stdio.h>
int main() {
int a = 10, b = 5;
// Arithmetic operations
printf("a + b = %d\n", a + b);
printf("a - b = %d\n", a - b);
printf("a * b = %d\n", a * b);
printf("a / b = %d\n", a / b);
printf("a %% b = %d\n", a % b);
// Relational operations
printf("a == b: %d\n", a == b);
printf("a > b: %d\n", a > b);
// Logical operations
printf("(a > 0) && (b > 0): %d\n", (a > 0) && (b > 0));
return 0;
}
Control statements determine the flow of execution in a program.
if (condition) {
// code to execute if condition is true
}
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
switch (expression) {
case constant1:
// code block
break;
case constant2:
// code block
break;
default:
// default code block
}
for (initialization; condition; increment) {
// code to be executed
}
while (condition) {
// code to be executed
}
do {
// code to be executed
} while (condition);
#include <stdio.h>
int main() {
int i;
// if-else example
int num = 10;
if (num > 0) {
printf("%d is positive\n", num);
} else {
printf("%d is not positive\n", num);
}
// for loop example
printf("Counting from 1 to 5: ");
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
printf("\n");
// while loop example
int count = 5;
printf("Countdown: ");
while (count > 0) {
printf("%d ", count);
count--;
}
printf("\n");
return 0;
}
Functions are self-contained blocks of code that perform a specific task.
return_type function_name(parameter_list) {
// function body
return value; // if return_type is not void
}
#include <stdio.h>
// Function declaration (prototype)
int add(int a, int b);
int main() {
int num1 = 5, num2 = 10;
int sum;
// Function call
sum = add(num1, num2);
printf("Sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
// Function definition
int add(int a, int b) {
return a + b;
}
printf("Hello, World!\n");
printf("Number: %d, Float: %.2f, Character: %c\n", 10, 3.14, 'A');
putchar('A'); // Outputs a single character
puts("Hello, World!"); // Outputs a string and adds a newline
int age;
float height;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your height: ");
scanf("%f", &height);
printf("Enter your name: ");
scanf("%s", name);
char ch;
printf("Enter a character: ");
ch = getchar(); // Reads a single character
char str[100];
// Unsafe - avoid using gets()
// gets(str);
// Safe alternative
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
Warning: Avoid using gets() as it doesn't check buffer boundaries and can lead to buffer overflow vulnerabilities. Always use fgets() instead.
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h> includes the standard input/output library
int main() defines the main function where program execution begins
printf("Hello, World!\n"); prints the message to the screenreturn 0; indicates successful program termination
#include <stdio.h>
int main() {
char operator;
double num1, num2, result;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
switch(operator) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
} else {
printf("Error: Division by zero\n");
return 1;
}
break;
default:
printf("Error: Invalid operator\n");
return 1;
}
printf("Result: %.2lf %c %.2lf = %.2lf\n", num1, operator, num2, result);
return 0;
}
The C compilation process converts human-readable source code into machine-executable code.
# Compile and link in one step
gcc program.c -o program
# Step-by-step compilation
gcc -E program.c -o program.i # Preprocessing
gcc -S program.i -o program.s # Compilation
gcc -c program.s -o program.o # Assembly
gcc program.o -o program # Linking
# Run the program
./program
Note: GCC (GNU Compiler Collection) is the most commonly used compiler for C programming on Linux and other platforms.
Programming Tip: The best way to learn C programming is by writing code regularly. Start with simple programs and gradually increase complexity.